package com.basic.modules.ykf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.basic.common.utils.ParamsModel;
import com.basic.common.utils.ParamUtils;
import com.basic.modules.sys.entity.SysFileEntity;
import com.basic.modules.sys.service.SysFileService;
import com.basic.modules.ykf.entity.YkfScriptManageEntity;
import com.basic.modules.ykf.model.YkfMaterialFileModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.basic.common.utils.PageUtils;
import com.basic.common.utils.Query;

import com.basic.modules.ykf.dao.YkfMaterialFileDao;
import com.basic.modules.ykf.entity.YkfMaterialFileEntity;
import com.basic.modules.ykf.service.YkfMaterialFileService;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("ykfMaterialFileService")
public class YkfMaterialFileServiceImpl extends ServiceImpl<YkfMaterialFileDao, YkfMaterialFileEntity> implements YkfMaterialFileService {

    @Autowired
    private SysFileService sysFileService;

    @Override
    public PageUtils queryPage(ParamsModel<YkfMaterialFileEntity> params) {
        if (ObjectUtil.isEmpty(params.getSort()) || StrUtil.isBlank(params.getSort().getSidx()) || StrUtil.isBlank(params.getSort().getOrder())) {
            ParamsModel.SortParams sortParams = new ParamsModel.SortParams();
            sortParams.setSidx("createTime");
            sortParams.setOrder("desc");
            params.setSort(sortParams);
        }
        QueryWrapper<YkfMaterialFileEntity> queryWrapper = ParamUtils.createQueryWrapperByDefaultSort(params);
        if (ObjectUtil.isNotEmpty(params) && ObjectUtil.isNotEmpty(params.getCondition()) && StrUtil.isNotBlank(params.getCondition().getKeyword())) {
            queryWrapper.lambda().like(YkfMaterialFileEntity::getName, params.getCondition().getKeyword());
        }
        IPage<YkfMaterialFileModel> resultPage = new Query<YkfMaterialFileModel>().getPage(BeanUtil.beanToMap(params.getPage()));
        IPage<YkfMaterialFileEntity> page = this.page(
                new Query<YkfMaterialFileEntity>().getPage(BeanUtil.beanToMap(params.getPage())),
                queryWrapper
        );
        if (ObjectUtil.isNotEmpty(page) && ObjectUtil.isNotEmpty(page.getRecords())) {
            List<YkfMaterialFileModel> resultList = new ArrayList<>();
            for (YkfMaterialFileEntity record : page.getRecords()) {
                YkfMaterialFileModel model = new YkfMaterialFileModel();
                SysFileEntity file = sysFileService.getById(record.getFileId());
                BeanUtil.copyProperties(record, model);
                if (ObjectUtil.isNotEmpty(file.getFileSize())) {
                    Map<String, Object> map = formatFileSize(file.getFileSize());
                    model.setFileSize((BigDecimal) map.get("fileSize"));
                    model.setFileSizeUnit((String) map.get("fileSizeUnit"));
                    model.setFileType(file.getFileType());
                    model.setFileUrl(file.getFileUrl());
                    resultList.add(model);
                }
            }
            resultPage.setRecords(resultList);
            resultPage.setPages(page.getPages());
            resultPage.setCurrent(page.getCurrent());
            resultPage.setTotal(page.getTotal());
            resultPage.setSize(page.getSize());
        }
        return new PageUtils(resultPage);
    }

    public static Map<String, Object> formatFileSize(BigDecimal bytes) {
        Map<String, Object> map = new HashMap<>();
        if (bytes.compareTo(new BigDecimal(1024)) < 0) {
            map.put("fileSize", bytes);
            map.put("fileSizeUnit", "B");
        } else if (bytes.compareTo(new BigDecimal(1024 * 1024)) < 0) {
            map.put("fileSize", bytes.divide(new BigDecimal(1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "K");
        } else if (bytes.compareTo(new BigDecimal(1024 * 1024 * 1024)) < 0) {
            map.put("fileSize", bytes.divide(new BigDecimal(1024 * 1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "M");
        } else {
            map.put("fileSize", bytes.divide(new BigDecimal(1024 * 1024 * 1024), 2, RoundingMode.HALF_UP));
            map.put("fileSizeUnit", "G");
        }
        return map;
    }

}
